//
//  HSPNewCarViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPNewCarViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
@interface HSPNewCarViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableViewNewCar;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *CarArray;
@end

@implementation HSPNewCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    [self tableViewCar];
    self.CarArray = [[NSMutableArray alloc]init];
    [self AlbumaHeader];
    [self AlbumaFooter];

    
}
- (void)tableViewCar
{
    _tableViewNewCar = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADJUSTHEIGHT * (667 - 147)) style:UITableViewStylePlain];
    _tableViewNewCar.delegate = self;
    _tableViewNewCar.dataSource =self;
    [self.view addSubview:_tableViewNewCar];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPNewCarViewController *vc = self;
    [_tableViewNewCar addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewNewCar.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPNewCarViewController *vcc = self;
    [_tableViewNewCar addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/2?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
            if (_isUp == YES) {
                [self.CarArray removeAllObjects];
            }
            for (NSDictionary *dica in [back objectForKey:@"data"]) {
                HSPNewCarModel *modelCar = [HSPNewCarModel modelWithDictionary:dica];
                [self.CarArray addObject:modelCar];
            }
            [self.tableViewNewCar reloadData];
            [self.tableViewNewCar.header endRefreshing];
            [self.tableViewNewCar.footer endRefreshing];
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CarArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReusableCell"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ReusableCell"];
    }
    HSPNewCarModel *model = self.CarArray[indexPath.row];
    self.tableViewNewCar.scrollsToTop = YES;
    [cell getCarImage:model];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
